<template>
  <div class="register-container">
    <div class="register-box">
      <!-- 角色切换（保持与登录页相同样式） -->
      <div class="role-toggle">
        <el-button :type="userForm.userType === 1 ? 'primary' : ''" @click="changeRgeType(1)">
          求职用户注册
        </el-button>
        <el-button :type="userForm.userType === 2 ? 'primary' : ''" @click="changeRgeType(2)">
          企业HR注册
        </el-button>
      </div>
      <!-- 用户注册表单 -->
      <div class="form-container">
        <el-form ref="userRuleRef" :rules="userRules" :model="userForm" label-width="100px">     
          <el-form-item label="用户名" prop="username">
            <el-input v-model="userForm.username" placeholder="设置用户名"/>
          </el-form-item>
          <el-form-item label="电话号码" prop="phone">
            <el-input v-model="userForm.phone" placeholder="请输入电话号码"/>
          </el-form-item>
          <el-form-item label="密码" prop="password">
            <el-input v-model="userForm.password" type="password" placeholder="设置密码"/>
          </el-form-item>
          <el-form-item label="确认密码" prop="confirmPwd">
            <el-input v-model="userForm.confirmPwd" type="password" placeholder="再次输入密码"/>
          </el-form-item>
        </el-form>
        <el-button type="primary" class="submit-btn" @click="register">
          {{ userForm.userType === 1 ? '注册求职账号' : '注册企业账号' }}
        </el-button>
        <p class="login-link">已有账号？<a href="/">去登录</a></p>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ElMessage } from 'element-plus'
import { ref } from 'vue'
import { registerApi } from '../api/register'
import { useRouter } from 'vue-router'

const router = useRouter()
// 用户表单验证规则
const userRuleRef = ref()
const userRules = ref({
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' },
    { min: 2, max: 20, message: '用户名长度应在2到20个字符之间', trigger: 'blur' }
  ],
  phone: [
    { required: true, message: '请输入手机号', trigger: 'blur' },
    { pattern: /^1[3-9]\d{9}$/, message: '请输入有效的手机号', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 6, max: 16, message: '密码长度应在6到16个字符之间', trigger: 'blur' },
  ],
  confirmPwd: [
    { required: true, message: '请再次输入密码', trigger: 'blur' },
    { 
      validator: (rule, value, callback) => {
        if (value !== userForm.value.password) {
          callback(new Error('两次输入的密码不一致'))
        } else {
          callback()
        }
      },
      trigger: 'blur'
    }
  ],
});
// 用户表单
const userForm = ref({
  username: '',
  password: '',
  confirmPwd: '',
  phone: '',
  userType: 1
})
// 切换用户类型（求职用户/企业用户）
const changeRgeType = (type) => {
  //清空表单数据
  userForm.value = { username: '', password: '', confirmPwd: '', phone: '', userType: 1 }
  userForm.value.userType = type
  if (userRuleRef.value){
    userRuleRef.value.resetFields()
  }
}
// 注册功能
const register = async () => { 
  /* 判断表单引用是否存在 */
  if(!userRuleRef.value) return
  userRuleRef.value.validate(async (valid) => {
    /* 判断表单验证是否通过 */
    if(valid){
      let result = await registerApi(userForm.value)   //验证通过执行新增
      if(result.code){
        ElMessage.success('注册成功！')
        router.push('/')
      }else{
        ElMessage.error(result.msg)
      }
    }else{
      ElMessage.error('表单验证不通过')  //表单验证失败提示信息
    }
  })
}
</script>

<style scoped>
/* 修改为与登录页面相同的背景样式 */
.register-container {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
  background: url('backgroundRegister.jpg') center/cover no-repeat;
  position: relative;
}

/* 添加背景遮罩层（与登录页一致） */
.register-container::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background-color: rgba(255, 255, 255, 0.7);
  z-index: 0;
}

.register-box {
  width: 400px;
  padding: 30px;
  background: rgba(255, 255, 255, 0.3); /* 半透明白色背景 */
  backdrop-filter: blur(3px); /* 毛玻璃效果 */
  border-radius: 8px;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.3);
  position: relative;
  z-index: 1;
}

.role-toggle {
  display: flex;
  justify-content: center;
  margin-bottom: 20px;
}

.role-toggle .el-button {
  margin: 0 10px;
  width: 150px;
}

.submit-btn {
  width: 100%;
  margin-top: 20px;
}

.login-link {
  text-align: center;
  margin-top: 15px;
  color: #666;
  font-size: 14px;
}

.login-link a {
  color: #409EFF;
  margin-left: 5px;
  text-decoration: none;
}

.login-link a:hover {
  text-decoration: underline;
}
</style>